### Code for outputting strategic response functions (best insincere voting as function of tau) and descriptives on tau (histograms)

D = read.csv("BES_all_including_all_taus_2005_2010_2015.csv", stringsAsFactors = F)
parties = c("lab", "con", "ld", "ukip", "grn", "snp", "pc")


source("utils_party_regularizing.r")

path_to_output = paste0("output/") # to allow output to be put somewhere else   
if(! dir.exists(path_to_output)){
  stop("Destination for output not found. Update 'path_to_output' variable in line 5.\n")
}

options(scipen = 999) # avoiding scientific notation in figures 

### histogram of tau

pdf(paste0(path_to_output, "hist_of_tau_default_bins.pdf"), width = 5.5, height = 4.5)
par(mfrow = c(1,1), mar = c(4,4,1,0))
hist(D$tau_party_feelmix_actual_85, col = rgb(.7, .7, .7, alpha = .5), xlab = substitute(paste(x1, tau, x2), list(x1 = "Tactical incentive (", x2 = ")")), main = "", xlim = c(-.00125, .00125))
dev.off()

library(ggplot2)
ggplot(data = D, aes(x = tau_party_feelmix_actual_85)) + 
  geom_histogram(boundary = 0, alpha = .5, color = "black") +
  labs(x = substitute(paste(x1, tau, x2), list(x1 = "Tactical incentive (", x2 = ")")), y = "Count")

ggsave(paste0(path_to_output, "hist_of_tau_gg.pdf"), width = 4.5, height = 4)


## size and tactical incentive by party

party.order = c("con", "snp", "lab", "ld", "pc", "ukip", "grn")
mat = matrix(NA, nrow = length(party.order)*3, ncol = 4)
years = c(2005, 2010, 2015)
colnames(mat) = c("party", "year", "prop.of.sample", "prop.pos.tau")
mat = as.data.frame(mat)
for(i in 1:length(party.order)){
  for(j in 1:length(years)){
    this.row = i + (j-1)*(length(party.order))
    mat[this.row, 1] = party.order[i]
    mat[this.row, 2] = years[j]
    mat[this.row, 3] = mean(D$party1_party_feelmix[D$year == years[j]] == party.order[i], na.rm = T)
    mat[this.row, 4] = mean((D$tau_party_feelmix_actual_85 > 0)[D$year == years[j] & D$party1_party_feelmix == party.order[i]], na.rm = T)
  }
}


pdf(paste0(path_to_output, "size_and_tactical_incentives_by_party_pref.pdf"), width = 6, height = 5)
par(mar = c(4, 4, 1,1))
plot(x = c(0, .4), y = c(0,1), type = "n", axes = F, xlab = "Proportion of sample (by year)", ylab = substitute(paste(x1, tau, x2), list(x1 = "Proportion with ", x2 = " > 0")))
axis(1); axis(2)
cols = rev(c("gray 20", "gray40", "gray70"))
for(j in 1:length(party.order)){
  lines(mat[mat$party == party.order[j], 3:4])
  points(mat[mat$party == party.order[j], 3:4], pch = 19, col = cols)
  text(mat[which(mat$party == party.order[j])[1], 3:4], pos = c(3, 4, 1, 3, 4, 1, 3)[j], labels = c("Con", "SNP", "Lab", "Lib\nDem", "Plaid\nCymru", "UKIP", "Green")[j]) 
}
legend("topright", pch = c(19, 19, 19), col = cols, legend = c("2005", "2010", "2015"))
dev.off()


#### plots of tactical voting by tau 

source("utils_plotting_functions.r") # for saturated functions 

# the Fisher comparison one 

# first we run the regressions and store the results 
D$vote.boo.tactical = (D$vote_code_party_feelmix_actual_85 == 2)
D$tau = D$tau_party_feelmix_actual_85
D$tau_cat = D$tau_party_feelmix_actual_85_cat
outcomes = c("vote.boo.tactical", "fisher.tactical")
reg.list = reg.list2 = reg.list3 = list()
for(j in 1:length(outcomes)){
  outcome = outcomes[j]
  reg.list[[j]] = lm(get(outcome) ~ as.factor(tau_cat) - 1, data = D)
  reg.list2[[j]] = lm(get(outcome) ~ as.factor(tau_cat) - 1, data = D)
  reg.list3[[j]] = lm(get(outcome) ~ as.factor(tau_cat) - 1, data = D)
}


s = 85
noc = 10
pdf(paste0(path_to_output, "tactical_measures_comparison_s_", s, "_saturated_10.pdf"), width = 7, height = 3)

par(mfrow = c(1,2), mar = c(4,5,1,1), cex = .9)

probs = seq(0, 1, length = noc + 1)
bin.means = c()
for(j in 1:10){bin.means = c(bin.means, mean(D$tau[D$tau_cat == j], na.rm = T))}

the.labs = c("Best\ninsincere\nvote", "Tactical vote\n(Fisher's\ndefinition)")
the.ylims = c(0, .7)
make.plot.of.saturated.regressions(reg.list, plot.confints = T, error.bar.confint.plot = F, x.vals = bin.means, offsets = rep(0, length(reg.list)), default.axis = T, x.lab = "Tactical incentive (10 bins)", y.line.loc = 0, pchs = c(19, 21), tones = c(.4, .6), y.lab = "Proportion", y.lims = the.ylims)
text(x = rep(max(bin.means), 2), y = c(coef(reg.list[[1]])[noc] + .1, coef(reg.list[[2]])[noc] - .1), adj = 1, labels = the.labs, cex = .6)
make.plot.of.saturated.regressions(reg.list, plot.confints = T, error.bar.confint.plot = F, x.lab = "", x.labels = rep("", noc), pchs = c(19, 21), tones = c(.4, .6), y.lab = "Proportion", y.lims = the.ylims)
mtext("Tactical incentive (10 bins, even spacing)", 1, line = 1.75, cex = .8)
text(x = rep(noc, 2), y = c(coef(reg.list[[1]])[noc] + .1, coef(reg.list[[2]])[noc] - .15), adj = 1, labels = the.labs, cex = .6)

dev.off()



### by social characteristic 

###############################################
####### Comparison SRFs #######################
###############################################

D$vote.insincere = D$vote_code_party_feelmix_actual_85 %in% c(2,3)
outcomes = c("vote.boo.tactical", "fisher.tactical", "vote.insincere")

# parameters for plotting
binary.offsets = c(-.05, .05)
ternary.offsets = c(-.075, 0, .075)

# a list of arguments for subsequent analysis and plotting
argument.list = list()
this.use = rep(T, nrow(D))
argument.list[["gender"]] = list(cat.mat = cbind(this.use & D$female == 1, this.use & D$female == 0), cat.labels = c("Women", "Men"), offsets = binary.offsets)
argument.list[["left_right"]] = list(cat.mat = cbind(this.use & D$left_party_feelmix == 1, this.use & D$left_party_feelmix == 0), cat.labels = c("Labour preferrer", "Conservative preferrer"), offsets = binary.offsets)
argument.list[["income"]] = list(cat.mat = cbind(this.use & D$incomex == 3, this.use & D$incomex == 2, this.use & D$incomex == 1), cat.labels = c("High income", "Medium income", "Low income"), offsets = ternary.offsets)
argument.list[["education"]] = list(cat.mat = cbind(this.use & D$educaltx == 3, this.use & D$educaltx == 2, this.use & D$educaltx == 1), cat.labels = c("Level 4+ (uni. degree)", "Level 3", "Level 1 or 2"), offsets = ternary.offsets)
argument.list[["age_group"]] = list(cat.mat = cbind(this.use & D$agegrpx == 3, this.use & D$agegrpx == 2, this.use & D$agegrpx == 1), cat.labels = c("60+", "30-59", "Below 30"), offsets = ternary.offsets)
argument.list[["election_year"]] = list(cat.mat = cbind(this.use & D$year == 2005, this.use & D$year == 2010, this.use & D$year == 2015), cat.labels = c("2005", "2010", "2015"), offsets = ternary.offsets)
argument.list[["number_not_missing"]] = list(cat.mat = cbind(this.use & D$how_many_not_missing_party_feelmix == 3, this.use & D$how_many_not_missing_party_feelmix > 3), cat.labels = c("Only three prefs", "More than three prefs"), offsets = binary.offsets)
argument.list[["number_not_missing_young_only"]] = list(cat.mat = cbind(this.use & D$how_many_not_missing_party_feelmix %in% 3:4 & D$agegrpx == 1, this.use & D$how_many_not_missing_party_feelmix > 4) & D$agegrpx == 1, cat.labels = c("Young and 3-4 prefs", "Young and > 4 prefs"), offsets = binary.offsets)
argument.list[["number_not_missing_old_only"]] = list(cat.mat = cbind(this.use & D$how_many_not_missing_party_feelmix %in% 3:4 & D$agegrpx == 3, this.use & D$how_many_not_missing_party_feelmix > 4) & D$agegrpx == 3, cat.labels = c("Old and 3-4 prefs", "Old and > 4 prefs"), offsets = binary.offsets)
argument.list[["stratindex.above.median"]] = list(cat.mat = cbind(this.use & D$stratindex.above.median == 0, this.use & D$stratindex.above.median == 1), cat.labels = c("Lower self-reported\nstrategic disposition", "Higher self-reported\nstrategic disposition"), offsets = binary.offsets)

nal = names(argument.list)
reg.list = list()
pchs = c(19, 21, 17)
noc = 10
ymax = .7 # c(1, .7)
for(i in 1:length(ymax)){
  this.ymax = ymax[i]
  for(j in 1:length(outcomes)){
    reg.list[[outcomes[j]]] = list()
    outcome = outcomes[j]
    for(k in 1:length(nal)){
      use.mat = argument.list[[nal[k]]]$cat.mat
      reg.list[[outcomes[j]]][[nal[k]]] = list()
      for(l in 1:ncol(use.mat)){
        reg.list[[outcomes[j]]][[nal[k]]][[l]] = lm(get(outcome) ~ as.factor(tau_cat) - 1, data = D[use.mat[,l],])			
      }
      # make the plots here? 
      pdf(paste0(path_to_output, "group_comparison_by_", nal[k], "_", gsub("\\.", "", outcome), "_srf_s_", s, "_saturated_10_bins.pdf"), width = 3.5, height = 3)
      this.reg.list = reg.list[[outcomes[j]]][[nal[k]]]
      
      par(mfrow = c(1,1), mar = c(4,5,1,1), cex = .9)
      
      make.plot.of.saturated.regressions(this.reg.list, plot.confints = T, error.bar.confint.plot = F, x.lab = "", x.labels = rep("", noc), pchs = pchs, tones = c(.4, .6, .8), offsets = c(0, 0, 0), y.lims = c(0, this.ymax), y.lab = "Proportion voting best insincere") 
      mtext("Tactical incentive (10 bins, even spacing)", 1, line = 1.75, cex = .8)
      legend("topleft", legend = argument.list[[nal[k]]]$cat.labels, pch = pchs[1:length(argument.list[[nal[k]]]$cat.labels)], col = "black", text.col = "black", lty = 1:length(this.reg.list), cex = .8)
      
      dev.off()
      
    }
  }	
}


##### robustness to alternative preference specifications in 2015 only

pref_types = c("party_feelmix", "party_feelpre", "party_feelpost", "leader_feelpost")

outcomes = c("vote.boo.tactical") 

for(pref_type in pref_types){
  
  D$vote.boo.tactical = (D[[paste0("vote_code_", pref_type, "_actual_85")]] == 2)
  D$tau = D[[paste0("tau_", pref_type, "_actual_85")]]
  D$tau_cat = as.factor(D[[paste0("tau_", pref_type, "_actual_85_cat")]])
  
  argument.list[["left_right"]] = list(cat.mat = cbind(this.use & D[[paste0("left_", pref_type)]] == 1, this.use & D[[paste0("left_", pref_type)]] == 0), cat.labels = c("Labour preferrer", "Conservative preferrer"), offsets = binary.offsets)
  
  nal = names(argument.list)
  reg.list = list()
  pchs = c(19, 21, 17)
  noc = 10
  ymax = .7 
  for(i in 1:length(ymax)){
    this.ymax = ymax[i]
    for(j in 1:length(outcomes)){
      reg.list[[outcomes[j]]] = list()
      outcome = outcomes[j]
      for(k in 1:length(nal)){
        if(k == 6){next}
        use.mat = argument.list[[nal[k]]]$cat.mat
        reg.list[[outcomes[j]]][[nal[k]]] = list()
        for(l in 1:ncol(use.mat)){
          reg.list[[outcomes[j]]][[nal[k]]][[l]] = lm(vote.boo.tactical ~ tau_cat - 1, data = D[use.mat[,l] & D$year == 2015,])			
        }
        # make the plots here? 
        pdf(paste0(path_to_output, "group_comparison_by_", nal[k], "_", gsub("\\.", "", outcome), "_srf_s_", s, "_saturated_10_bins_", pref_type, ".pdf"), width = 3.5, height = 3)
        this.reg.list = reg.list[[outcomes[j]]][[nal[k]]]
        
        par(mfrow = c(1,1), mar = c(4,5,1,1), cex = .9)
        
        make.plot.of.saturated.regressions(this.reg.list, plot.confints = T, error.bar.confint.plot = F, x.lab = "", x.labels = rep("", noc), pchs = pchs, tones = c(.4, .6, .8), offsets = c(0, 0, 0), y.lims = c(0, this.ymax), y.line.loc = min(as.numeric(D$tau_cat[D$tau > 0]), na.rm = T) - .5, y.lab = "Proportion voting best insincere") 
        mtext("Tactical incentive (10 bins, even spacing)", 1, line = 1.75, cex = .8)
        legend("topleft", legend = argument.list[[nal[k]]]$cat.labels, pch = pchs[1:length(argument.list[[nal[k]]]$cat.labels)], col = "black", text.col = "black", lty = 1:length(this.reg.list), cex = .8)
        
        dev.off()
        
      }
    }	
  }
}



